﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FarseerGames.FarseerPhysics.Collisions;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_2DFramwork
{
    public abstract class VisualObject:GameObject
    {
        public static List<VisualObject> _lVisualObject;
        public Texture2D Texture;
        public Body Body;
        public Geom Geom;
        public int Width;
        public int Height;
        public Vector2 Origin;
        public Vector2 Position;
        public SpriteBatch SpriteBatch;
        public ObjectType Type;
        public PhysicsSimulator PhysicsSimulator;
       
        public VisualObject(Game Game):base(Game)
        {
            //SpriteBatch = new SpriteBatch (Game.GraphicsDevice); 
        }
        public abstract void Draw();
        
     
        public void MoveToPoint(Vector2 point,List<PathNode> list)
        {
            //List<PathNode> list = new List<PathNode>();
            for (int index = 0; index < list.Count; index++)
            {
                PathNode t = list[index];
                switch (t.Command)
                {
                    case MovementCommad.Walk:
                        {
                            Walk(t.Point);
                            break;
                        }
                    case MovementCommad.Run:
                        {
                            Run(t.Point);
                            break;
                        }
                    case MovementCommad.Jump:
                        {
                            Jump(t.Point);
                            break;
                        }
                }
            }
        }

        public void Walk(Vector2 goTo)
        {
            while (Body.Position.X != goTo.X || Body.Position.Y != goTo.Y)
            {
                if (Body.Position.X > goTo.X)
                    Body.Position = new Vector2(Body.Position.X - 1, Body.Position.Y);
                else if (Body.Position.X != goTo.X)
                    Body.Position = new Vector2(Body.Position.X + 1, Body.Position.Y);

                if (Body.Position.Y > goTo.Y)
                    Body.Position = new Vector2(Body.Position.X, Body.Position.Y - 1);
                else if (Body.Position.Y != goTo.Y)
                    Body.Position = new Vector2(Body.Position.X, Body.Position.Y + 1);

            }
        }

        public void Run(Vector2 goTo)
        {
            while (Body.Position.X != goTo.X || Body.Position.Y != goTo.Y)
            {
                if (Body.Position.X > goTo.X)
                    Body.Position = new Vector2(Body.Position.X - 5, Body.Position.Y);
                else if (Body.Position.X != goTo.X)
                    Body.Position = new Vector2(Body.Position.X + 5, Body.Position.Y);

                if (Body.Position.Y > goTo.Y)
                    Body.Position = new Vector2(Body.Position.X, Body.Position.Y - 5);
                else if (Body.Position.Y != goTo.Y)
                    Body.Position = new Vector2(Body.Position.X, Body.Position.Y + 5);

            }
        }

        public void Jump(Vector2 goTo)
        {
            Body.ApplyForce(goTo);
        }

        public virtual void Move(float x, float y)
        {
            Position.X += x;
            Position.Y += y;
        }
        public static bool IsCollide(ref Geom Object1, ref Geom Object2)
        {
            if (AABB.Intersect(ref Object1.AABB, ref Object2.AABB))
                return true;
            return false;
        }
    }
}
